/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CompraVIEW.java
 *
 * Created on Oct 17, 2012, 3:56:09 PM
 */
package view;

import dao.CompraDAO;
import dao.FornecedorDAO;
import dao.ListaCompraDAO;
import dao.ProdutoDAO;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import model.Compra;
import model.Fornecedor;
import model.ListaCompra;
import model.Produto;

/**
 *
 * @author Klecio
 */
public class CompraVIEW extends javax.swing.JDialog {

    /** Creates new form CompraVIEW */
    public CompraVIEW(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        ImageIcon icone = new ImageIcon("src/icons/entrada64.PNG");   
        setIconImage(icone.getImage());
        initComponents();
        capturaFornecedor();
        dateEntrada.setDate(new Date());
    }

    public void capturaFornecedor(){
        FornecedorDAO fornecedorDao = new FornecedorDAO();
        Fornecedor uf = new Fornecedor();
        Vector<Fornecedor> vector = fornecedorDao.listaTodos();
        int quant = vector.size();
        for(int i = 0; i < quant; i++){
            cbFornecedor.addItem(vector.get(i).getRazao());
        }
    }
    
    BigDecimal totalProdutos = new BigDecimal(0.00);
    
    public void preencheTabela(String descricao, BigDecimal quant, BigDecimal valorUnit, BigDecimal valorTotal){        
        DefaultTableModel modelo = (DefaultTableModel) tabelaProdutos.getModel();
        modelo.addRow(new Object[]{
            descricao,
            quant,
            valorUnit,
            valorTotal
        });
        totalProdutos = totalProdutos.add(valorTotal);
        lbProdutos.setText(String.valueOf(totalProdutos));
    }
    
    BigDecimal ipi;
    BigDecimal icms;
    BigDecimal seguro;
    BigDecimal frete;
    BigDecimal outras;
    BigDecimal total;
    
    public void calculaTotal(){
        ipi = new BigDecimal(tfIPI.getText());
        icms = new BigDecimal(tfICMS.getText());
        seguro = new BigDecimal(tfSeguro.getText());
        frete = new BigDecimal(tfFrete.getText());
        outras = new BigDecimal(tfOutras.getText());
        total = (((ipi.add(icms)).add(seguro)).add(frete)).add(outras);
        lbTotal.setText(String.valueOf(total));
    }
   
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel6 = new javax.swing.JPanel();
        jLabel13 = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        tfNumero = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        cbFornecedor = new javax.swing.JComboBox();
        btNovoFornecedor = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabelaProdutos = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        dateEmissao = new com.toedter.calendar.JDateChooser();
        dateEntrada = new com.toedter.calendar.JDateChooser();
        jPanel4 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        lbProdutos = new javax.swing.JLabel();
        lbTotal = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        tfOutras = new javax.swing.JTextField();
        tfSeguro = new javax.swing.JTextField();
        tfFrete = new javax.swing.JTextField();
        tfICMS = new javax.swing.JTextField();
        tfIPI = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jBConfirmar = new javax.swing.JButton();
        jBCancelar = new javax.swing.JButton();
        jBNovoItem = new javax.swing.JButton();
        jBRemover = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Compra");

        jPanel6.setBackground(new java.awt.Color(153, 153, 153));

        jLabel13.setFont(new java.awt.Font("Cooper Black", 0, 24));
        jLabel13.setForeground(java.awt.Color.orange);
        jLabel13.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel13.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/entrada64.png"))); // NOI18N
        jLabel13.setText("ENTRADA DE PRODUTOS");
        jLabel13.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, 734, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jLabel13, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Nº da Nota"));

        tfNumero.setFont(new java.awt.Font("Tahoma", 1, 18));
        tfNumero.setForeground(new java.awt.Color(153, 0, 0));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(tfNumero, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(tfNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 18, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Fornecedor"));

        cbFornecedor.setFont(new java.awt.Font("Tahoma", 0, 14));

        btNovoFornecedor.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/novo.png"))); // NOI18N
        btNovoFornecedor.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNovoFornecedorActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbFornecedor, 0, 236, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btNovoFornecedor, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(cbFornecedor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE)
                    .addComponent(btNovoFornecedor, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 45, Short.MAX_VALUE))
                .addGap(12, 12, 12))
        );

        tabelaProdutos.setFont(new java.awt.Font("Tahoma", 0, 16)); // NOI18N
        tabelaProdutos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Descrição", "Quantidade", "Valor Unitário", "Valor Total"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabelaProdutos.setRowHeight(25);
        jScrollPane1.setViewportView(tabelaProdutos);
        tabelaProdutos.getColumnModel().getColumn(0).setResizable(false);
        tabelaProdutos.getColumnModel().getColumn(0).setPreferredWidth(400);
        tabelaProdutos.getColumnModel().getColumn(1).setResizable(false);
        tabelaProdutos.getColumnModel().getColumn(1).setPreferredWidth(50);
        tabelaProdutos.getColumnModel().getColumn(2).setResizable(false);
        tabelaProdutos.getColumnModel().getColumn(3).setResizable(false);

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Data"));

        jLabel1.setText("Data da emissão:");

        jLabel2.setText("Data da entrada:");

        dateEntrada.setForeground(new java.awt.Color(153, 0, 0));
        dateEntrada.setEnabled(false);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(dateEntrada, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE)
                    .addComponent(dateEmissao, javax.swing.GroupLayout.DEFAULT_SIZE, 113, Short.MAX_VALUE))
                .addGap(20, 20, 20))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(dateEmissao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(dateEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        jPanel4.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "TOTAL", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 12))); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("Valor dos Produtos:");

        jLabel9.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel9.setText("TOTAL GERAL:");

        lbProdutos.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lbProdutos.setForeground(new java.awt.Color(0, 0, 204));
        lbProdutos.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbProdutos.setText("0.00");

        lbTotal.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lbTotal.setForeground(new java.awt.Color(0, 0, 204));
        lbTotal.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lbTotal.setText("0.00");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addGap(90, 90, 90)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel9)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbTotal, javax.swing.GroupLayout.DEFAULT_SIZE, 92, Short.MAX_VALUE))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lbProdutos, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(lbProdutos, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel9)
                    .addComponent(lbTotal))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder("Tributos"));

        tfOutras.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tfOutras.setText("0");
        tfOutras.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfOutrasFocusLost(evt);
            }
        });

        tfSeguro.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tfSeguro.setText("0");
        tfSeguro.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfSeguroFocusLost(evt);
            }
        });

        tfFrete.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tfFrete.setText("0");
        tfFrete.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfFreteFocusLost(evt);
            }
        });

        tfICMS.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tfICMS.setText("0");
        tfICMS.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfICMSFocusLost(evt);
            }
        });

        tfIPI.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        tfIPI.setText("0");
        tfIPI.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                tfIPIFocusLost(evt);
            }
        });

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel8.setText("Outras despesas:");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel7.setText("Frete:");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel6.setText("Seguro:");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel5.setText("ICMS:");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("IPI:");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel7)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(tfIPI, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfICMS, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel6)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfSeguro, javax.swing.GroupLayout.DEFAULT_SIZE, 70, Short.MAX_VALUE))
                    .addGroup(jPanel5Layout.createSequentialGroup()
                        .addComponent(tfFrete, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 88, Short.MAX_VALUE)
                        .addComponent(jLabel8)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(tfOutras, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(24, 24, 24))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(tfIPI, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(tfICMS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6)
                    .addComponent(tfSeguro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(tfFrete, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(tfOutras, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        jBConfirmar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/confirmar.png"))); // NOI18N
        jBConfirmar.setText("Confirmar");
        jBConfirmar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBConfirmarActionPerformed(evt);
            }
        });

        jBCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/cancelar.png"))); // NOI18N
        jBCancelar.setText("Cancelar");
        jBCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCancelarActionPerformed(evt);
            }
        });

        jBNovoItem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/add.png"))); // NOI18N
        jBNovoItem.setText("Novo Item");
        jBNovoItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBNovoItemActionPerformed(evt);
            }
        });

        jBRemover.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icons/delete.png"))); // NOI18N
        jBRemover.setText("Remover");
        jBRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBRemoverActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 734, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 11, Short.MAX_VALUE)
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(8, 8, 8))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBNovoItem)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jBRemover)
                        .addGap(0, 498, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
            .addComponent(jPanel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGap(251, 251, 251)
                .addComponent(jBConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jBCancelar)
                .addContainerGap(256, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanel6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBNovoItem)
                    .addComponent(jBRemover))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, 73, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jBCancelar)
                    .addComponent(jBConfirmar))
                .addContainerGap())
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void jBNovoItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBNovoItemActionPerformed
        NovoItemVIEW novoItem = new NovoItemVIEW(null, rootPaneCheckingEnabled);
        novoItem.setVisible(true);
        if(!novoItem.descricao.equals("")){
            preencheTabela(novoItem.descricao, novoItem.quant, novoItem.valorUnit, novoItem.valorTotal);                     
        }
    }//GEN-LAST:event_jBNovoItemActionPerformed

    private void jBCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_jBCancelarActionPerformed

    private void btNovoFornecedorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btNovoFornecedorActionPerformed
        FornecedorVIEW fornecedorVIEW = new FornecedorVIEW(null, rootPaneCheckingEnabled);
        fornecedorVIEW.setVisible(true);
        capturaFornecedor();
    }//GEN-LAST:event_btNovoFornecedorActionPerformed

    private void jBRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBRemoverActionPerformed
        if(tabelaProdutos.getSelectedRow() < 0){
            JOptionPane.showMessageDialog(null, "Selecione um item a ser excluído!!!");
        }else{
            DefaultTableModel modelo = (DefaultTableModel) tabelaProdutos.getModel();
            int linha = tabelaProdutos.getSelectedRow();
            BigDecimal total = (BigDecimal) modelo.getValueAt(linha, 3);
            totalProdutos = totalProdutos.subtract(total);
            lbProdutos.setText(String.valueOf(totalProdutos));
            modelo.removeRow(linha);
        }        
    }//GEN-LAST:event_jBRemoverActionPerformed

    private void tfIPIFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfIPIFocusLost
        calculaTotal();
    }//GEN-LAST:event_tfIPIFocusLost

    private void tfICMSFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfICMSFocusLost
        calculaTotal();
    }//GEN-LAST:event_tfICMSFocusLost

    private void tfSeguroFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfSeguroFocusLost
        calculaTotal();
    }//GEN-LAST:event_tfSeguroFocusLost

    private void tfFreteFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfFreteFocusLost
        calculaTotal();
    }//GEN-LAST:event_tfFreteFocusLost

    private void tfOutrasFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_tfOutrasFocusLost
        calculaTotal();
    }//GEN-LAST:event_tfOutrasFocusLost

    private void jBConfirmarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBConfirmarActionPerformed
        FornecedorDAO fornecedorDAO = new FornecedorDAO();
        Fornecedor fornecedor = new Fornecedor();
        fornecedor = fornecedorDAO.buscarPorFornecedor((String) cbFornecedor.getSelectedItem());
        
        CompraDAO compraDAO = new CompraDAO();
        Compra compra = new Compra();
        compra.setCodFornecedor(fornecedor.getIdFornecedor());
        compra.setNumeroNf(Integer.parseInt(tfNumero.getText()));
        compra.setDataEmissao(dateEmissao.getDate());
        compra.setDataEntrada(dateEntrada.getDate());
        BigDecimal icms = new BigDecimal(tfICMS.getText());
        compra.setValorIcms(icms);
        BigDecimal ipi = new BigDecimal(tfIPI.getText());
        compra.setValorIpi(ipi);
        BigDecimal seguro = new BigDecimal(tfSeguro.getText());
        compra.setValorSeguro(seguro);
        BigDecimal frete = new BigDecimal(tfFrete.getText());
        compra.setValorFrete(frete);
        BigDecimal outras = new BigDecimal(tfOutras.getText());
        compra.setOutrasDespesas(outras);
        BigDecimal totalProdutos = new BigDecimal(lbProdutos.getText());
        compra.setTotalProdutos(totalProdutos);
        BigDecimal totalNota = new BigDecimal(lbTotal.getText());
        compra.setTotalNota(totalNota);
        
        Produto produto = new Produto();
        ProdutoDAO produtoDAO = new ProdutoDAO();        
        
        ListaCompra listaCompra = new ListaCompra();
        ListaCompraDAO listaCompraDAO = new ListaCompraDAO();
        ArrayList<ListaCompra> array = new ArrayList<ListaCompra>();
        int linhas = tabelaProdutos.getRowCount();
        for(int i = 0; i < linhas; i++){
            String descricao = (String) tabelaProdutos.getValueAt(i, 0);
            produto = produtoDAO.buscarPorDescricao(descricao);
            BigDecimal qtde = (BigDecimal) tabelaProdutos.getValueAt(i, 1);
            BigDecimal vlrUnit = (BigDecimal) tabelaProdutos.getValueAt(i, 2);
            BigDecimal vlrTotal = (BigDecimal) tabelaProdutos.getValueAt(i, 3);
            listaCompra.setCodLista(1);
            listaCompra.setCodProduto(produto.getIdProduto());
            listaCompra.setQuantidade(qtde);
            listaCompra.setValorTotal(vlrTotal);
            listaCompra.setValorUnitario(vlrUnit);
            //array[i] = listaCompra;
            //listaCompraDAO.salvar(listaCompra);            
            //System.out.println(produto.getIdProduto() + " " + produto.getDescricao());
        }
        
        for(int i = 0; i < array.size(); i++){
            System.out.println(array.get(i).getCodProduto());
        }
                
        compra.setCodListaProdutos(listaCompraDAO.PegaUltimoRegistro() - 1);        
        compraDAO.salvar(compra);
    }//GEN-LAST:event_jBConfirmarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                CompraVIEW dialog = new CompraVIEW(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btNovoFornecedor;
    private javax.swing.JComboBox cbFornecedor;
    private com.toedter.calendar.JDateChooser dateEmissao;
    private com.toedter.calendar.JDateChooser dateEntrada;
    private javax.swing.JButton jBCancelar;
    private javax.swing.JButton jBConfirmar;
    private javax.swing.JButton jBNovoItem;
    private javax.swing.JButton jBRemover;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbProdutos;
    private javax.swing.JLabel lbTotal;
    private javax.swing.JTable tabelaProdutos;
    private javax.swing.JTextField tfFrete;
    private javax.swing.JTextField tfICMS;
    private javax.swing.JTextField tfIPI;
    private javax.swing.JTextField tfNumero;
    private javax.swing.JTextField tfOutras;
    private javax.swing.JTextField tfSeguro;
    // End of variables declaration//GEN-END:variables
}
